package com.example.job.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.job.model.JobModel;
import com.example.job.service.JobModelService;
import com.example.utils.StringUtils;

import java.util.List;

/**
 * @Classname JobModelServiceImpl
 * @Description TODO
 * @Date 2022/8/2 21:27
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class JobModelServiceImpl implements JobModelService {

    @Autowired
    TableService tableService;

    @Override
    public JobModel save(JobModel jobModel) {
        tableService.save(jobModel,JobModel.class);
        return jobModel;
    }

    @Override
    public JobModel updateById(JobModel jobModel) {
        tableService.updateById(jobModel,JobModel.class);
        return jobModel;
    }

    @Override
    public IPage<JobModel> page(int pageNum, int pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(JobModel.class);
        if(object != null){
            Object name=object.get("name");
            if(!StringUtils.isEmpty(name)){
                sqlBuilder.and("name").like(name);
            }
            Object jobName=object.get("jobName");
            if (!StringUtils.isEmpty(jobName)) {
                sqlBuilder.and("jobName").like(jobName);
            }
            Object jobGroup=object.get("jobGroup");
            if (!StringUtils.isEmpty(jobGroup)) {
                sqlBuilder.and("jobGroup").like(jobGroup);
            }
            Object clazz=object.get("clazz");
            if (!StringUtils.isEmpty(clazz)) {
                sqlBuilder.and("clazz").like(clazz);
            }
            Object descnote=object.get("descnote");
            if (!StringUtils.isEmpty(descnote)) {
                sqlBuilder.and("descnote").like(descnote);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        IPage<JobModel> iPage=tableService.getPage(sqlBuilder,JobModel.class);
        return iPage;
    }

    @Override
    public JobModel getById(Object id) {
        JobModel jobModel=tableService.getOneById(id,JobModel.class);
        return jobModel;
    }

    @Override
    public List<JobModel> getJobModelList() {
        List<JobModel> jobModelList=tableService.getList(JobModel.class);
        return jobModelList;
    }

    @Override
    public JobModel getJobModelorJSONObject(JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(JobModel.class);
        if(object!=null){
            Object jobName=object.get("jobName");
            if(!StringUtils.isEmpty(jobName)){
                sqlBuilder.or("jobName").eq(jobName);
            }
            Object jobGroupName=object.get("jobGroupName");
            if (!StringUtils.isEmpty(jobGroupName)) {
                sqlBuilder.or("jobGroupName").eq(jobGroupName);
            }
            Object tiggerName=object.get("tiggerName");
            if (!StringUtils.isEmpty(tiggerName)) {
                sqlBuilder.or("tiggerName").eq(tiggerName);
            }
            Object tiggerGroup=object.get("tiggerGroup");
            if (!StringUtils.isEmpty(tiggerGroup)) {
                sqlBuilder.or("tiggerGroup").eq(tiggerGroup);
            }
        }
        JobModel jobModel=tableService.selectOne(sqlBuilder,JobModel.class);
        return jobModel;
    }
}
